<?php

namespace app\api\model;



use app\api\model\shop\Shop;
use app\api\model\shop\ShopFans;
use app\common\model\MoneyLog;
use app\common\model\User;
use app\shopapi\model\ExtendConsumeShop;
use app\shopapi\model\ShopUser;
use think\Db;
use think\Model;
use traits\model\SoftDelete;


class UserConsumeLog extends Model
{
    // 表名
    protected $name = 'user_consume_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'cps_type_text',
        'consume_text',
    ];

    //消费类型
    public function consume_type()
    {
        return [0=>'线下买单',1=>'折扣买单',2=>'线上买单',3=>'团购',4=>'霸王餐'];
    }

    //线上买单类型
    public function cps_type()
    {
        return [0=>'京东',1=>'拼多多',2=>'唯品会',3=>'美团',4=>'饿了么',5=>'滴滴',6=>'抖音',7=>'淘宝',8=>'飞猪',9=>'霸王餐'];
    }

    public function getConsumeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['consume_type'] ?? '');
        $list  = $this->consume_type();
        return $list[$value] ?? '';
    }

    public function getCpsTypeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['cps_type'] ?? '');
        $list  = $this->cps_type();
        return $list[$value] ?? '';
    }

    //预估收益
    public function income($user_id,$start_time,$end_time,$consume_type=-1)
    {
        $total_where = ['user_id'=>$user_id,'is_refund'=>0];
        $total_where['createtime'] = ['between', [$start_time, $end_time]];
        $total_promotion_where = ['a.user_id'=>$user_id,'ucl.is_refund'=>0];
        $total_promotion_where['ucl.createtime'] = ['between', [$start_time, $end_time]];
        if ($consume_type>=0){
            $total_where['consume_type'] = $consume_type;
            $total_promotion_where['ucl.consume_type'] = $consume_type;
        }
        $estimated_income = UserConsumeLog::where($total_where)->sum('commission');
        $promotion_estimated_income = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where($total_promotion_where)->sum('a.commission');
        $total_estimated_income = bcadd($promotion_estimated_income,$estimated_income,4);

        return $total_estimated_income;
    }

    //订单数
    public function order_count($user_id,$start_time,$end_time,$consume_type=-1)
    {
        $total_where = ['user_id'=>$user_id,'is_refund'=>0];
        $total_where['createtime'] = ['between', [$start_time, $end_time]];
        $total_promotion_where = ['a.user_id'=>$user_id,'ucl.is_refund'=>0];
        $total_promotion_where['ucl.createtime'] = ['between', [$start_time, $end_time]];
        if ($consume_type>=0){
            $total_where['consume_type'] = $consume_type;
            $total_promotion_where['ucl.consume_type'] = $consume_type;
        }
        $count = UserConsumeLog::where($total_where)->count();
        $promotion_count = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where($total_promotion_where)->count();
        $total_count = bcadd($count,$promotion_count);

        return $total_count;
    }
    //成交额
    public function turnover($user_id,$start_time,$end_time,$consume_type=-1)
    {
        $total_where = ['user_id'=>$user_id,'is_refund'=>0];
        $total_where['createtime'] = ['between', [$start_time, $end_time]];
        $total_promotion_where = ['a.user_id'=>$user_id,'ucl.is_refund'=>0];
        $total_promotion_where['ucl.createtime'] = ['between', [$start_time, $end_time]];
        if ($consume_type>=0){
            $total_where['consume_type'] = $consume_type;
            $total_promotion_where['ucl.consume_type'] = $consume_type;
        }
        $turnover = UserConsumeLog::where($total_where)->sum('pay_price');
        $promotion_turnover = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where($total_promotion_where)->sum('ucl.pay_price');
        $total_turnover = bcadd($turnover,$promotion_turnover,4);

        return $total_turnover;
    }

    //收益数据
    public function stat($param)
    {
        $time_type = $param['time_type'];
        $user_id = $param['user_id'];

        $user = User::get($user_id);

        if ($time_type==0){
            //今天
            $start_time =  strtotime("today");
            $end_time =  strtotime("tomorrow -1 second")-1;
        } elseif ($time_type==1){
            //近七天
            $start_time = strtotime("-7 day");
            $end_time = time();
        } elseif ($time_type==2){
            //近三十天
            $start_time = strtotime("-30 day");
            $end_time = time();
        } else {
            //筛选时间 不能大于三个月
            $start_time = isset($param['start_time'])?strtotime($param['start_time']):'';
            $end_time = isset($param['end_time'])?strtotime($param['end_time'])+86400:'';
        }

        //累计到账收益
        $total_money = MoneyLog::where(['user_id'=>$user_id,'type'=>1])->sum('money');
        //$total_money = $user->total_money;
        //累计成交额
        $turnover = UserConsumeLog::where(['user_id'=>$user_id])->sum('pay_price');
        $promotion_turnover = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where(['a.user_id'=>$user_id])->sum('pay_price');
        //$total_turnover = $turnover+$promotion_turnover;
        //累计受众人  指能带来佣金回流的粉丝,包括你的粉丝,地推,店铺粉丝等
        $fans_count = User::where(['parent_id'=>$user_id])->count();

        $people_count = $fans_count*2;

        //待到账金额
        /*$stay_account = UserConsumeLog::where(['user_id'=>$user_id,'is_refund'=>0,'is_to_account'=>0])->sum('commission');
        $promotion_stay_account = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where(['a.user_id'=>$user_id,'ucl.is_refund'=>0,'ucl.is_to_account'=>0])->sum('a.commission');*/

        //总
        $total_estimated_income = $this->income($user_id,$start_time,$end_time);
        $total_order_count = $this->order_count($user_id,$start_time,$end_time);
        $total_turnover = $this->turnover($user_id,$start_time,$end_time);
        //线上
        $total_online_estimated_income = $this->income($user_id,$start_time,$end_time,2);
        $total_online_order_count = $this->order_count($user_id,$start_time,$end_time,2);
        $total_online_turnover = $this->turnover($user_id,$start_time,$end_time,2);
        //线下
        $total_offline_estimated_income = $this->income($user_id,$start_time,$end_time,0);
        $total_offline_order_count = $this->order_count($user_id,$start_time,$end_time,0);
        $total_offline_turnover = $this->turnover($user_id,$start_time,$end_time,0);
        //折扣
        $total_discount_estimated_income = $this->income($user_id,$start_time,$end_time,1);
        $total_discount_order_count = $this->order_count($user_id,$start_time,$end_time,1);
        $total_discount_turnover = $this->turnover($user_id,$start_time,$end_time,1);

        //霸王餐
        $overlord_meal_order_count = $this->order_count($user_id,$start_time,$end_time,4);
        $overlord_meal_turnover = $this->turnover($user_id,$start_time,$end_time,4);
        $overlord_meal_estimated_income = $this->income($user_id,$start_time,$end_time,4);
        $data = [
            'total_money'=>$total_money,
            'turnover'=>bcadd($turnover,$promotion_turnover,4),
            'stay_account'=>$this->forecastCommission($user_id,4),
            //'stay_account'=>bcadd($stay_account,$promotion_stay_account,4),
            'people_count'=>$people_count,

            'total_estimated_income'=>$total_estimated_income,
            'total_order_count'=>$total_order_count,
            'total_turnover'=>$total_turnover,

            'online_estimated_income'=>$total_online_estimated_income,
            'online_order_count'=>$total_online_order_count,
            'online_turnover'=>$total_online_turnover,

            'offline_estimated_income'=>$total_offline_estimated_income,
            'offline_order_count'=>$total_offline_order_count,
            'offline_turnover'=>$total_offline_turnover,

            'discount_estimated_income'=>$total_discount_estimated_income,
            'discount_order_count'=>$total_discount_order_count,
            'discount_turnover'=>$total_discount_turnover,

            'overlord_meal_order_count'=>$overlord_meal_order_count,
            'overlord_meal_turnover'=>$overlord_meal_turnover,
            'overlord_meal_estimated_income'=>$overlord_meal_estimated_income,
        ];

        return $data;
    }

    //我的消费记录
    public function lists($user_id,$status)
    {
        $where = ['user_id'=>$user_id];
        if ($status==0){
            $where['is_refund'] = 0;
        } else {
            $where['is_refund'] = 1;
        }
        $lists = UserConsumeLog::where($where)
            ->order('pay_time desc')
            ->paginate(10, false, ['query' => request()->get()]);

        return $lists;
    }

    //推广订单-用户端
    public function extendorder($user_id,$status,$keyword='')
    {
        $where = ['a.user_id'=>$user_id];
        if ($status==0){
            $where['ucl.is_refund'] = 0;
        } else {
            $where['ucl.is_refund'] = 1;
        }
        if(!empty($keyword)){
            $where['ucl.shop_name|ucl.order_no'] = ['like',"%$keyword%"];
        }

        $lists = ExtendConsume::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where($where)
            ->field('a.*,ucl.consume_id,ucl.title,ucl.image,ucl.shop_id,ucl.shop_name,ucl.order_no,ucl.pay_type,ucl.pay_time,
            ucl.is_to_account,ucl.account_time,ucl.pay_price,ucl.total_commission,ucl.consume_type,ucl.cps_type,ucl.order_status_name,
            ucl.is_refund,ucl.send_total_commission')
            ->order('ucl.pay_time desc')
            ->paginate(10, false, ['query' => request()->get()]);
        return $lists;
    }

    //推广订单-商户端
    public function extendorderShop($shop_id,$status,$keyword='')
    {
        $where = ['a.shop_id'=>$shop_id];
        if ($status==0){
            $where['ucl.is_refund'] = 0;
        } else {
            $where['ucl.is_refund'] = 1;
        }
        if(!empty($keyword)){
            $where['ucl.shop_name|ucl.order_no'] = ['like',"%$keyword%"];
        }

        $lists = ExtendConsumeShop::alias('a')
            ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
            ->where($where)
            ->field('a.*,ucl.consume_id,ucl.title,ucl.image,ucl.shop_id,ucl.shop_name,ucl.order_no,ucl.pay_type,ucl.pay_time,
            ucl.is_to_account,ucl.account_time,ucl.pay_price,ucl.total_commission,ucl.consume_type,ucl.cps_type,ucl.order_status_name,
            ucl.is_refund,ucl.send_total_commission')
            ->order('ucl.pay_time desc')
            ->paginate(10, false, ['query' => request()->get()]);
        return $lists;
    }

    //发放用户自己的佣金
    public static function sendCommission($order_no,$goods_id='')
    {
        if ($goods_id){
            $info = UserConsumeLog::get(['order_no'=>$order_no,'goods_id'=>$goods_id,'is_to_account'=>0]);
        } else {
            $info = UserConsumeLog::get(['order_no'=>$order_no,'is_to_account'=>0]);
        }

        if (!$info){
            return false;
        }

        Db::startTrans();
        try {
            $info->is_to_account = 1;
            $info->account_time = time();
            $info->order_status_name = '已到账';
            $res = $info->save();

            if (!$res){
                Db::rollback();
                return false;
            }
            User::money($info->commission,$info->user_id,'收益已到账,交易编号:'.$order_no,1);
            ExtendConsume::sendCommission($info->consume_id);
            ExtendConsumeShop::sendCommission($info->consume_id);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }


    //预估佣金
    //type 0是累计到账,1今日直推,2昨日直推,3本月直推
    public function forecastCommission($user_id,$type=0)
    {
        $where = ['user_id'=>$user_id];
        $promotion_where = ['a.user_id'=>$user_id];
        if ($type==0){
            $where['is_to_account'] = 1;
            $promotion_where['ucl.is_to_account'] = 1;

            $commission = UserConsumeLog::where($where)->sum('commission');

            $promotion_stay_account = ExtendConsume::alias('a')
                ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
                ->where($promotion_where)
                ->sum('ucl.commission');

            return bcadd($commission,$promotion_stay_account,4);
        } else {
            // 当天开始时间戳
            $todayStart = strtotime("today");
            // 当天结束时间戳
            $todayEnd = strtotime("tomorrow") - 1;

            // 昨天开始时间戳
            $yesterdayStart = strtotime("yesterday");
            // 昨天结束时间戳
            $yesterdayEnd = strtotime("today") - 1;

            // 当月开始时间戳
            $monthStart = strtotime(date('Y-m-01 00:00:00'));
            // 当月结束时间戳
            $monthEnd = strtotime(date('Y-m-t 23:59:59'));


            if ($type==1){
                $where['createtime'] = ['between', [$todayStart, $todayEnd]];
                $promotion_where['ucl.createtime'] = ['between', [$todayStart, $todayEnd]];
            } elseif ($type==2){
                $where['createtime'] = ['between', [$yesterdayStart, $yesterdayEnd]];
                $promotion_where['ucl.createtime'] = ['between', [$yesterdayStart, $yesterdayEnd]];
            } elseif ($type==3){
                $where['createtime'] = ['between', [$monthStart, $monthEnd]];
                $promotion_where['ucl.createtime'] = ['between', [$monthStart, $monthEnd]];
            } elseif ($type==4){
                $where['is_to_account'] = 0;
                $where['is_refund'] = 0;
                $promotion_where['ucl.is_to_account'] = 0;
                $promotion_where['ucl.is_refund'] = 0;
            }

            $commission = UserConsumeLog::where($where)->sum('commission');

            $promotion_stay_account = ExtendConsume::alias('a')
                ->join('user_consume_log ucl','a.consume_log_id = ucl.consume_id')
                ->where($promotion_where)
                ->sum('a.commission');
            //dump($where);
            //dump($promotion_where);die;
            return bcadd($commission,$promotion_stay_account,4);
            //return $commission;
        }

    }


    //美团外卖卡卷佣金计算-根据消费人如是*店铺的粉丝,该店铺有推广人,该推广人的奖励
    public static function waimaiCouponFansConsumeCalculate($user_id,$total_commission,$consume_log_id)
    {
        $shop_fans = ShopFans::where(['user_id'=>$user_id])->find();

        if ($shop_fans){
            $shop_info = Shop::alias('s')
                ->join('shop_user su','su.id = s.user_id')
                ->where(['s.shop_id'=>$shop_fans['shop_id']])
                ->field('su.recommender_id')
                ->find();

            if ($shop_info){

                $recommender = User::where(['id'=>$shop_info['recommender_id']])
                    ->field('id,level,parent_id')->find();

                if ($recommender){
                    $extend_user_ids = [];//所有有收益的用户
                    if ($recommender->level==2||$recommender->level==3){
                        $recommender_commission = $total_commission*0.18;
                        ExtendConsume::addLog($consume_log_id,$recommender->id,$recommender_commission);
                    }
                    $res = self::diffLastArr($recommender->id);
                    $last_arr = $res['list'];

                    $v3_weizhi = 0;
                    $v3_id = 0;
                    foreach ($last_arr as $k=>$v){
                        if ($v['level']==3&&$v['id']!=$user_id){
                            $v3_weizhi = $k;
                            $v3_id = $v['id'];
                            break;
                        }
                    }
                    //一级v1-二级v1
                    $parent = User::where(['id'=>$recommender->parent_id,'level'=>1])->field('id,level,parent_id')->find();
                    if ($parent){
                        $first_level_1_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                        array_push($extend_user_ids,$parent->id);
                        $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>1])->field('id,level,parent_id')->find();
                        if ($parent_2){
                            $second_level_1_user_commission = $total_commission*0.01;
                            ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                            array_push($extend_user_ids,$parent_2->id);
                        }
                    }

                    //一级v2-二级v2
                    $first_level_2_user = self::getIds($recommender->parent_id,2);

                    if ($first_level_2_user){
                        $first_level_2_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$first_level_2_user->id,$first_level_2_user_commission);
                        array_push($extend_user_ids,$first_level_2_user->id);
                        $second_level_2_user = self::getIds($first_level_2_user->parent_id,2);
                        if ($second_level_2_user){
                            $second_level_2_user_commission = $total_commission*0.01;
                            ExtendConsume::addLog($consume_log_id,$second_level_2_user->id,$second_level_2_user_commission);
                            array_push($extend_user_ids,$second_level_2_user->id);
                        }
                    }

                    $v3_child_list = array_splice($last_arr,0,$v3_weizhi);
                    $new_v3_child_list = array();
                    // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
                    foreach ($v3_child_list as $key => $info) {
                        $new_v3_child_list[$info['level']][] = $info;
                    }

                    //dump($new_v3_child_list);
                    $level_1_arr = array_key_exists(1,$new_v3_child_list)?$new_v3_child_list[1]:[];
                    $level_2_arr = array_key_exists(2,$new_v3_child_list)?$new_v3_child_list[2]:[];


                    if (!empty($level_1_arr)){
                        $level_1_key = array_search($user_id, array_column($level_1_arr, 'id'));

                        //所有v1数量大于2证明当前用户是2级外v1
                        if (count($level_1_arr)>2){
                            $commission = $total_commission*0.01;
                        } elseif(count($level_1_arr)==2) {
                            //二级v1
                            $commission = $total_commission*0.02;
                        } elseif(count($level_1_arr)==1) {
                            //一级v1
                            $commission = $total_commission*0.2;
                        }

                        if (!is_bool($level_1_key)){
                            ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                            array_push($extend_user_ids,$v3_id);
                        }
                    }
                    if (!empty($level_2_arr)){
                        $level_2_key = array_search($user_id, array_column($level_2_arr, 'id'));

                        //所有v2数量大于2证明当前用户是2级外v2,否则就是
                        if (count($level_2_arr)>2){
                            $commission = $total_commission*0.01;
                        }  elseif(count($level_2_arr)==2) {
                            //二级v2
                            $commission = $total_commission*0.02;
                        } elseif(count($level_2_arr)==1) {
                            //一级v2
                            $commission = $total_commission*0.2;
                        }
                        if (!is_bool($level_2_key)){
                            ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                            array_push($extend_user_ids,$v3_id);
                        }

                    }

                    if ($recommender->level==3){
                        $first_level_3_user = self::getIds($recommender->parent_id,3);

                        if ($first_level_3_user){
                            $first_level_3_user_commission = $total_commission*0.02;
                            ExtendConsume::addLog($consume_log_id,$first_level_3_user->id,$first_level_3_user_commission);
                            array_push($extend_user_ids,$first_level_3_user->id);
                            //查查当前用户的二级v3
                            $second_level_3_user = self::getIds($first_level_3_user->parent_id,3);
                            if ($second_level_3_user){
                                $second_level_3_user_commission = $total_commission*0.01;
                                ExtendConsume::addLog($consume_log_id,$second_level_3_user->id,$second_level_3_user_commission);
                                array_push($extend_user_ids,$second_level_3_user->id);
                            }
                        }
                    }
                    return $extend_user_ids;
                }
                return false;
            }
            return false;
        }
        return false;
    }

    //美团外卖卡卷佣金计算
    public static function waimaiCouponConsumeCalculate($user_id,$total_commission,$consume_log_id)
    {
        $user = User::get($user_id);

        $send_total_commission = 0;//累计有收益的所有收益

        $extend_user_ids = [];//所有有收益的用户
        if ($user->parent_id>0){
            $res = self::diffLastArr($user_id);
            $last_arr = $res['list'];
            $v3_weizhi = 0;
            $v3_id = 0;

            foreach ($last_arr as $k=>$v){
                if ($v['level']==3&&$v['id']!=$user_id){
                    $v3_weizhi = $k;
                    $v3_id = $v['id'];
                    break;
                }
            }
            //-----------------------------------------查询当前用户的上级和上上级
            if ($user->level==1){
                $parent = User::where(['id'=>$user->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                if ($parent){
                    $first_level_1_user_commission = $total_commission*0.2;
                    ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                    $send_total_commission+=$first_level_1_user_commission;
                    array_push($extend_user_ids,$parent->id);

                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        $second_level_1_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        $send_total_commission+=$second_level_1_user_commission;
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }

            if ($user->level==2){
                $parent = User::where(['id'=>$user->parent_id])->field('id,level,parent_id')->find();

                if ($parent){
                    if ($parent->level==2){
                        $first_level_1_user_commission = $total_commission*0.2;
                        ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                        $send_total_commission+=$first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);
                    }


                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>2])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        $second_level_1_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        $send_total_commission+=$second_level_1_user_commission;
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }

            //-----------------------------------------针对v3发放的奖励
            $v3_child_list = array_splice($last_arr,0,$v3_weizhi);
            $new_v3_child_list = array();
            // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
            foreach ($v3_child_list as $key => $info) {
                $new_v3_child_list[$info['level']][] = $info;
            }

            $level_1_arr = array_key_exists(1,$new_v3_child_list)?$new_v3_child_list[1]:[];
            $level_2_arr = array_key_exists(2,$new_v3_child_list)?$new_v3_child_list[2]:[];

            if (!empty($level_1_arr)){
                $level_1_key = array_search($user_id, array_column($level_1_arr, 'id'));

                //所有v1数量大于2证明当前用户是2级外v1
                if (count($level_1_arr)>2){
                    $commission = $total_commission*0.26;
                } elseif(count($level_1_arr)==2) {
                    //二级v1
                    $commission = $total_commission*0.28;
                } elseif(count($level_1_arr)==1) {
                    //一级v1
                    $commission = $total_commission*0.48;
                }

                if (!is_bool($level_1_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }
            }
            if (!empty($level_2_arr)){
                $level_2_key = array_search($user_id, array_column($level_2_arr, 'id'));

                //所有v2数量大于2证明当前用户是2级外v2,否则就是
                if (count($level_2_arr)>2){
                    $commission = $total_commission*0.26;
                }  elseif(count($level_2_arr)==2) {
                    //二级v2
                    $commission = $total_commission*0.28;
                } elseif(count($level_2_arr)==1) {
                    //一级v2
                    $commission = $total_commission*0.48;
                }
                if (!is_bool($level_2_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }

            }
            if ($v3_id){
                $v3_user = User::get($v3_id);
                $first_level_3_user = self::getIds($v3_user->parent_id,3);
                if ($first_level_3_user){
                    $first_level_3_user_commission = $total_commission*0.04;
                    ExtendConsume::addLog($consume_log_id,$first_level_3_user->id,$first_level_3_user_commission);
                    $send_total_commission+=$first_level_3_user_commission;
                    array_push($extend_user_ids,$first_level_3_user->id);
                    //查查当前用户的二级v3
                    $second_level_3_user = self::getIds($first_level_3_user->parent_id,3);
                    if ($second_level_3_user){
                        $second_level_3_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$second_level_3_user->id,$second_level_3_user_commission);
                        $send_total_commission+=$second_level_3_user_commission;
                        array_push($extend_user_ids,$second_level_3_user->id);
                    }
                }
            }


            /*$res = UserConsumeLog::update([
                'send_total_commission'=>$send_total_commission,
                'extend_user_ids'=>json_encode($extend_user_ids,320)
            ],['id'=>$consume_log_id]);*/

            return true;

        }
        return false;
    }


    public static function oldwaimaiCouponConsumeCalculate($user_id,$total_commission,$consume_log_id)
    {
        $user = User::get($user_id);

        $send_total_commission = 0;//累计有收益的所有收益

        $extend_user_ids = [];//所有有收益的用户

        if ($user->parent_id>0){
            $res = self::diffLastArr($user_id);

            //$diff_last_arr = $res['diff_arr'];
            $last_arr = $res['list'];
            //dump($last_arr);
            $v3_weizhi = 0;
            $v3_id = 0;
            $v2_weizhi = 0;
            $v2_id = 0;
            $v1_weizhi = 0;
            $v1_id = 0;
            foreach ($last_arr as $k=>$v){
                if ($v['level']==3&&$v['id']!=$user_id){
                    $v3_weizhi = $k;
                    $v3_id = $v['id'];
                    break;
                }
            }
            foreach ($last_arr as $k=>$v){
                if ($v['level']==2&&$v['id']!=$user_id){
                    $v2_weizhi = $k;
                    $v2_id = $v['id'];
                    break;
                }
            }

            foreach ($last_arr as $k=>$v){
                if ($v['level']==1&&$v['id']!=$user_id){
                    $v1_weizhi = $k;
                    $v1_id = $v['id'];
                    break;
                }
            }

            //-----------------------------------------针对v1发放的奖励
            if ($user->level==1){
                $parent = User::where(['id'=>$user->parent_id,'level'=>1])->field('id,level,parent_id')->find();
                if ($parent){
                    $first_level_1_user_commission = $total_commission*0.2;
                    ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                    $send_total_commission+=$first_level_1_user_commission;
                    array_push($extend_user_ids,$parent->id);

                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>1])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        $second_level_1_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        $send_total_commission+=$second_level_1_user_commission;
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }


            //-----------------------------------------针对v2发放的奖励
            if ($user->level==2){
                $first_level_2_user = self::getIds($user->parent_id,2);

                if ($first_level_2_user){
                    $first_level_2_user_commission = $total_commission*0.2;
                    ExtendConsume::addLog($consume_log_id,$first_level_2_user->id,$first_level_2_user_commission);
                    $send_total_commission+=$first_level_2_user_commission;
                    array_push($extend_user_ids,$first_level_2_user->id);

                    $second_level_2_user = self::getIds($first_level_2_user->parent_id,2);
                    if ($second_level_2_user){
                        $second_level_2_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$second_level_2_user->id,$second_level_2_user_commission);
                        $send_total_commission+=$second_level_2_user_commission;
                        array_push($extend_user_ids,$second_level_2_user->id);
                    }
                }
            }


            //-----------------------------------------针对v3发放的奖励
            $v3_child_list = array_splice($last_arr,0,$v3_weizhi);
            $new_v3_child_list = array();
            // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
            foreach ($v3_child_list as $key => $info) {
                $new_v3_child_list[$info['level']][] = $info;
            }

            //dump($new_v3_child_list);
            $level_1_arr = array_key_exists(1,$new_v3_child_list)?$new_v3_child_list[1]:[];
            $level_2_arr = array_key_exists(2,$new_v3_child_list)?$new_v3_child_list[2]:[];
            //$level_3_arr = array_key_exists(3,$new_v3_child_list)?$new_v3_child_list[3]:[];

            //die;

            if (!empty($level_1_arr)){
                $level_1_key = array_search($user_id, array_column($level_1_arr, 'id'));

                //所有v1数量大于2证明当前用户是2级外v1
                if (count($level_1_arr)>2){
                    $commission = $total_commission*0.26;
                } elseif(count($level_1_arr)==2) {
                    //二级v1
                    $commission = $total_commission*0.28;
                } elseif(count($level_1_arr)==1) {
                    //一级v1
                    $commission = $total_commission*0.48;
                }

                if (!is_bool($level_1_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }
            }
            if (!empty($level_2_arr)){
                $level_2_key = array_search($user_id, array_column($level_2_arr, 'id'));

                //所有v2数量大于2证明当前用户是2级外v2,否则就是
                if (count($level_2_arr)>2){
                    $commission = $total_commission*0.26;
                }  elseif(count($level_2_arr)==2) {
                    //二级v2
                    $commission = $total_commission*0.28;
                } elseif(count($level_2_arr)==1) {
                    //一级v2
                    $commission = $total_commission*0.48;
                }
                if (!is_bool($level_2_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }

            }
            if ($user->level==3){
                $first_level_3_user = self::getIds($user->parent_id,3);

                if ($first_level_3_user){
                    $first_level_3_user_commission = $total_commission*0.04;
                    ExtendConsume::addLog($consume_log_id,$first_level_3_user->id,$first_level_3_user_commission);
                    $send_total_commission+=$first_level_3_user_commission;
                    array_push($extend_user_ids,$first_level_3_user->id);
                    //查查当前用户的二级v3
                    $second_level_3_user = self::getIds($first_level_3_user->parent_id,3);
                    if ($second_level_3_user){
                        $second_level_3_user_commission = $total_commission*0.02;
                        ExtendConsume::addLog($consume_log_id,$second_level_3_user->id,$second_level_3_user_commission);
                        $send_total_commission+=$second_level_3_user_commission;
                        array_push($extend_user_ids,$second_level_3_user->id);
                    }
                }
            }

            /*$res = UserConsumeLog::update([
                'send_total_commission'=>$send_total_commission,
                'extend_user_ids'=>json_encode($extend_user_ids,320)
            ],['id'=>$consume_log_id]);*/

            return true;
        }
        return false;
    }

    //锁会员收益计算
    //$user_id 消费人用户id
    //$total_commission 产生的所有佣金
    //$consume_log_id 用户的消费记录id
    //type 0是cps,1是折扣,2是线下
    public static function lockMemberConsumeCalculate($user_id,$total_commission,$consume_log_id,$type)
    {
        $user = User::get($user_id);

        $send_total_commission = 0;//累计有收益的所有收益

        $extend_user_ids = [];//所有有收益的用户

        if ($user->parent_id>0){
            $res = self::diffLastArr($user_id);

            $last_arr = $res['list'];
            
            $v3_weizhi = 0;
            $v3_id = 0;
        
            foreach ($last_arr as $k=>$v){
                if ($v['level']==3&&$v['id']!=$user_id){
                    $v3_weizhi = $k;
                    $v3_id = $v['id'];
                    break;
                }
            }

            
            if ($type==0){
                $first_level_commission_rate = 0.04;//一级v1佣金比例
                $second_level_commission_rate = 0.02;//二级v1佣金比例
            } elseif ($type==1){
                $first_level_commission_rate = 0.04;//一级v1佣金比例
                $second_level_commission_rate = 0.01;//二级v1佣金比例
            } elseif ($type==2){
                $first_level_commission_rate = 0.02;//一级v1佣金比例
                $second_level_commission_rate = 0.01;//二级v1佣金比例
            }
            //-----------------------------------------查询当前用户的上级和上上级
            if ($user->level==1){
                $parent = User::where(['id'=>$user->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                if ($parent){
                    $first_level_1_user_commission = $total_commission*$first_level_commission_rate;
                    ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                    $send_total_commission+=$first_level_1_user_commission;
                    array_push($extend_user_ids,$parent->id);

                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        $second_level_1_user_commission = $total_commission*$second_level_commission_rate;
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        $send_total_commission+=$second_level_1_user_commission;
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }

            if ($user->level==2){
                $parent = User::where(['id'=>$user->parent_id])->field('id,level,parent_id')->find();

                if ($parent){
                    if ($parent->level==2){
                        $first_level_1_user_commission = $total_commission*$first_level_commission_rate;
                        ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                        $send_total_commission+=$first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);
                    }


                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>2])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        $second_level_1_user_commission = $total_commission*$second_level_commission_rate;
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        $send_total_commission+=$second_level_1_user_commission;
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }

            //-----------------------------------------针对v3发放的奖励
            /*$v3_child_list = array_splice($last_arr,0,$v3_weizhi);
            $new_v3_child_list = array();
            // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
            foreach ($v3_child_list as $key => $info) {
                $new_v3_child_list[$info['level']][] = $info;
            }*/
            
            //if (/* condition */) {
                // code...
            //}
            if ($v3_id){
                if ($v3_weizhi>2) {
                    //二级外
                    if ($type==0){
                        $rate = 0.16;
                    } elseif ($type==1){
                        $rate = 0.08;
                    } elseif ($type==2){
                        $rate = 0.08;
                    }
                    $commission = $total_commission*$rate;

                    //$send_total_commission+=$commission;
                } elseif($v3_weizhi==2){
                    //二级v1v2
                    if ($type==0){
                        $rate = 0.18;
                    } elseif ($type==1){
                        $rate = 0.09;
                    } elseif ($type==2){
                        $rate = 0.09;
                    }
                    $commission = $total_commission*$rate;
                } elseif($v3_weizhi==1){
                    //一级v1v2
                    if ($type==0){
                        $rate = 0.22;
                    } elseif ($type==1){
                        $rate = 0.13;
                    } elseif ($type==2){
                        $rate = 0.11;
                    }
                    $commission = $total_commission*$rate;
                }
                ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
            }


            //dump($new_v3_child_list);
            //$level_1_arr = array_key_exists(1,$new_v3_child_list)?$new_v3_child_list[1]:[];
            //$level_2_arr = array_key_exists(2,$new_v3_child_list)?$new_v3_child_list[2]:[];
            //$level_3_arr = array_key_exists(3,$new_v3_child_list)?$new_v3_child_list[3]:[];

            //die;

            /*if (!empty($level_1_arr)){
                $level_1_key = array_search($user_id, array_column($level_1_arr, 'id'));

                //所有v1数量大于2证明当前用户是2级外v1
                if (count($level_1_arr)>2){
                    if ($type==0){
                        $rate = 0.16;
                    } elseif ($type==1){
                        $rate = 0.08;
                    } elseif ($type==2){
                        $rate = 0.08;
                    }
                    $commission = $total_commission*$rate;
                } elseif(count($level_1_arr)==2) {
                    //二级v1
                    if ($type==0){
                        $rate = 0.18;
                    } elseif ($type==1){
                        $rate = 0.09;
                    } elseif ($type==2){
                        $rate = 0.09;
                    }
                    $commission = $total_commission*$rate;
                } elseif(count($level_1_arr)==1) {
                    //一级v1
                    if ($type==0){
                        $rate = 0.22;
                    } elseif ($type==1){
                        $rate = 0.13;
                    } elseif ($type==2){
                        $rate = 0.11;
                    }
                    $commission = $total_commission*$rate;
                }

                if (!is_bool($level_1_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }
            }
            if (!empty($level_2_arr)){
                $level_2_key = array_search($user_id, array_column($level_2_arr, 'id'));

                //所有v2数量大于2证明当前用户是2级外v2,否则就是
                if (count($level_2_arr)>2){
                    if ($type==0){
                        $rate = 0.16;
                    } elseif ($type==1){
                        $rate = 0.08;
                    } elseif ($type==2){
                        $rate = 0.08;
                    }
                    $commission = $total_commission*$rate;
                }  elseif(count($level_2_arr)==2) {
                    //二级v2
                    if ($type==0){
                        $rate = 0.18;
                    } elseif ($type==1){
                        $rate = 0.09;
                    } elseif ($type==2){
                        $rate = 0.09;
                    }
                    $commission = $total_commission*$rate;
                } elseif(count($level_2_arr)==1) {
                    //一级v1
                    if ($type==0){
                        $rate = 0.22;
                    } elseif ($type==1){
                        $rate = 0.13;
                    } elseif ($type==2){
                        $rate = 0.11;
                    }
                    $commission = $total_commission*$rate;
                }
                if (!is_bool($level_2_key)){
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    $send_total_commission+=$commission;
                    array_push($extend_user_ids,$v3_id);
                }

            }*/


            if ($v3_id){
                $v3_user = User::get($v3_id);

                $first_level_3_user = self::getIds($v3_user->parent_id,3);
                if ($type==0){
                    $first_level_3_commission_rate = 0.04;//一级v3佣金比例
                    $second_level_3_commission_rate = 0.02;//二级v3佣金比例
                } elseif ($type==1){
                    $first_level_3_commission_rate = 0.02;//一级v3佣金比例
                    $second_level_3_commission_rate = 0.01;//二级v3佣金比例
                } elseif ($type==2){
                    $first_level_3_commission_rate = 0.02;//一级v3佣金比例
                    $second_level_3_commission_rate = 0.01;//二级v3佣金比例
                }
                if ($first_level_3_user){
                    $first_level_3_user_commission = $total_commission*$first_level_3_commission_rate;
                    ExtendConsume::addLog($consume_log_id,$first_level_3_user->id,$first_level_3_user_commission);
                    $send_total_commission+=$first_level_3_user_commission;
                    array_push($extend_user_ids,$first_level_3_user->id);
                    //查查当前用户的二级v3
                    $second_level_3_user = self::getIds($first_level_3_user->parent_id,3);
                    if ($second_level_3_user){
                        $second_level_3_user_commission = $total_commission*$second_level_3_commission_rate;
                        ExtendConsume::addLog($consume_log_id,$second_level_3_user->id,$second_level_3_user_commission);
                        $send_total_commission+=$second_level_3_user_commission;
                        array_push($extend_user_ids,$second_level_3_user->id);
                    }
                }
            }

            /*$res = UserConsumeLog::update([
                'send_total_commission'=>$send_total_commission,
                'extend_user_ids'=>json_encode($extend_user_ids,320)
            ],['id'=>$consume_log_id]);*/

            return true;
        }
        return false;
    }


    //推商家收益-线上废弃
    public static function recommendShopConsumeOnlineCalculate($user_id,$total_commission,$consume_log_id)
    {
        //查询属于哪个店铺的粉丝
        $shop_fans = ShopFans::where(['user_id'=>$user_id])->find();

        if ($shop_fans){
            $shop_info = Shop::alias('s')
                ->join('shop_user su','su.id = s.user_id')
                ->where(['s.shop_id'=>$shop_fans['shop_id']])
                ->field('su.recommender_id,s.shop_id')
                ->find();

            if ($shop_info){
                //发放店铺的18%
                $shop_commission = $total_commission*0.18;
                ExtendConsumeShop::addLog($consume_log_id,$shop_info['shop_id'],$shop_commission);

                //查询所属店铺的推广人
                $recommender = User::where(['id'=>$shop_info['recommender_id']])
                    ->field('id,level,parent_id')->find();
                if ($recommender){
                    $recommender_commission = 0;
                    if ($recommender['level']==2){
                        //推广人得4%
                        $recommender_commission = $total_commission*0.04;
                    } elseif($recommender['level']==3) {
                        //推广人得15%
                        $recommender_commission = $total_commission*0.15;
                    }
                    //推广人的佣金,
                    ExtendConsume::addLog($consume_log_id,$recommender['id'],$recommender_commission);

                    if ($recommender->level==2){
                        //一级v2
                        $parent = User::where(['id'=>$recommender->parent_id,'level'=>2])->find();
                        if ($parent){
                            $first_level2_commission = $total_commission*0.02;
                            ExtendConsume::addLog($consume_log_id,$parent['id'],$first_level2_commission);
                            $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>2])->find();
                            //二级v2
                            if ($parent_2){
                                $second_level2_commission = $total_commission*0.01;
                                ExtendConsume::addLog($consume_log_id,$parent_2['id'],$second_level2_commission);
                            }
                        }
                    }

                    $res = self::diffLastArr($recommender['id']);
                    $last_arr = $res['list'];
                    $v3_weizhi = 0;
                    $v3_id = 0;

                    foreach ($last_arr as $k=>$v){
                        if ($v['level']==3&&$v['id']!=$user_id){
                            $v3_weizhi = $k;
                            $v3_id = $v['id'];
                            break;
                        }
                    }

                    //-----------------------------------------针对v3发放的奖励
                    $v3_child_list = array_splice($last_arr,0,$v3_weizhi);
                    $new_v3_child_list = array();
                    // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
                    foreach ($v3_child_list as $key => $info) {
                        $new_v3_child_list[$info['level']][] = $info;
                    }

                    $level_2_arr = array_key_exists(2,$new_v3_child_list)?$new_v3_child_list[2]:[];

                    if (!empty($level_2_arr)){
                        $level_2_key = array_search($user_id, array_column($level_2_arr, 'id'));

                        //所有v2数量大于2证明当前用户是2级外v2,否则就是
                        if (count($level_2_arr)>2){
                            $commission = $total_commission*0.08;
                        }  elseif(count($level_2_arr)==2) {
                            //二级v2
                            $commission = $total_commission*0.09;
                        } elseif(count($level_2_arr)==1) {
                            //一级v2
                            $commission = $total_commission*0.11;
                        }
                        if (!is_bool($level_2_key)){
                            ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                            array_push($extend_user_ids,$v3_id);
                        }

                    }
                    if ($v3_id){
                        $v3_user = User::get($v3_id);
                        $first_level_3_user = self::getIds($v3_user->parent_id,3);
                        if ($first_level_3_user){
                            $first_level_3_user_commission = $total_commission*0.02;
                            ExtendConsume::addLog($consume_log_id,$first_level_3_user->id,$first_level_3_user_commission);

                            array_push($extend_user_ids,$first_level_3_user->id);
                            //查查当前用户的二级v3
                            $second_level_3_user = self::getIds($first_level_3_user->parent_id,3);
                            if ($second_level_3_user){
                                $second_level_3_user_commission = $total_commission*0.01;
                                ExtendConsume::addLog($consume_log_id,$second_level_3_user->id,$second_level_3_user_commission);
                                array_push($extend_user_ids,$second_level_3_user->id);
                            }
                        }
                    }

                }
                return false;
            }
            return false;
        }
        return false;

    }


    /**本店-折扣买单
     * Notes:推商家收益-线下和折扣买单 type0是线下  1是折扣
     * $shop_id  是在哪个商家消费的商户id
     * User: gai
     * Date: 2024/8/14
     * Time: 9:21
     * 会员到本店 指的是推广人HA推广了店铺ABC,*用户只要在店铺ABC消费就属于到本店
     * 会员到任何店  指的是店铺ABC绑的会员,去其他店铺消费,对于推广人HA来说属于到任何店
     */
    public static function newrecommendShopConsumeCalculate($user_id,$shop_id,$total_commission,$consume_log_id,$type=1)
    {
        //查询属于哪个店铺的粉丝
        //$shop_fans = ShopFans::where(['user_id'=>$user_id])->find();
        //if (!$shop_fans) return false;

        $shop_info = Shop::alias('s')
            ->join('shop_user su','su.id = s.user_id')
            ->where(['s.shop_id'=>$shop_id])
            ->field('su.recommender_id,s.shop_id')
            ->find();

        if ($type==1){
            //折扣
            $recommender_self_shop_commission = $total_commission*0.1;
            $first_level_1_user_commission = $total_commission*0.01;
            $second_level_1_user_commission = $total_commission*0.005;
            $v3_recommender_self_shop_commission = $total_commission*0.195;
            $v3_first_level_1_user_commission = $total_commission*0.02;
            $v3_second_level_1_user_commission = $total_commission*0.01;
            $second_rate_outside = 0.08;
            $second_rate = 0.085;
            $first_rate = 0.095;
        } elseif($type==0) {
            $recommender_self_shop_commission = $total_commission*0.56;
            $first_level_1_user_commission = $total_commission*0.02;
            $second_level_1_user_commission = $total_commission*0.005;
            $v3_recommender_self_shop_commission = $total_commission*0.67;
            $v3_first_level_1_user_commission = $total_commission*0.02;
            $v3_second_level_1_user_commission = $total_commission*0.01;
            $second_rate_outside = 0.08;
            $second_rate = 0.09;
            $first_rate = 0.11;
        } else {
            $recommender_self_shop_commission = $total_commission*0.04;
            $first_level_1_user_commission = $total_commission*0.02;
            $second_level_1_user_commission = $total_commission*0.01;

            $v3_recommender_self_shop_commission = $total_commission*0.15;
            $v3_first_level_1_user_commission = $total_commission*0.02;
            $v3_second_level_1_user_commission = $total_commission*0.01;

            $second_rate_outside = 0.08;
            $second_rate = 0.09;
            $first_rate = 0.11;

            $shop_commission = $total_commission*0.18;
            ExtendConsumeShop::addLog($consume_log_id,$shop_info['shop_id'],$shop_commission);
        }
        $send_total_commission = 0;
        $extend_user_ids = [];
        if ($shop_info){

            //查询所属店铺的推广人
            $recommender = User::where(['id'=>$shop_info['recommender_id']])
                ->field('id,level,parent_id')->find();

            if ($recommender){

                $res = self::diffLastArr($recommender->id);

                $last_arr = $res['list'];

                $v3_weizhi = 0;
                $v3_id = 0;

                foreach ($last_arr as $k=>$v){
                    if ($v['level']==3&&$v['id']!=$recommender->id){
                        $v3_weizhi = $k;
                        $v3_id = $v['id'];
                        break;
                    }
                }

                if ($recommender->level==2){
                    //当前推广人收益

                    /*if ($shop_id==$shop_info['shop_id']){
                        $recommender_self_shop_commission = $total_commission*0.10;
                    } else {
                        $recommender_self_shop_commission = $total_commission*0.04;
                    }*/
                    //$recommender_self_shop_commission = $total_commission*0.10;
                    ExtendConsume::addLog($consume_log_id,$recommender['id'],$recommender_self_shop_commission);

                    //查当前推广人属于*的一级v2
                    $parent = User::where(['id'=>$recommender->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                    if ($parent){

                        ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                        $send_total_commission+=$first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);

                        $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                        //查当前推广人属于*的二级v2
                        if ($parent_2){

                            ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                            $send_total_commission+=$second_level_1_user_commission;
                            array_push($extend_user_ids,$parent_2->id);
                        }
                    }

                    if ($v3_id){
                        if ($v3_weizhi>2) {
                            //二级外
                            $rate = $second_rate_outside;

                            $commission = $total_commission*$rate;

                            //$send_total_commission+=$commission;
                        } elseif($v3_weizhi==2){
                            //二级v1v2
                            $rate = $second_rate;
                            $commission = $total_commission*$rate;
                        } elseif($v3_weizhi==1){
                            //一级v1v2
                            $rate = $first_rate;
                            $commission = $total_commission*$rate;
                        }
                        ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    }

                } elseif ($recommender->level==3){
                    //v3 只有一级v3二级v3吃,
                    ExtendConsume::addLog($consume_log_id,$recommender['id'],$v3_recommender_self_shop_commission);

                    //查当前推广人属于*的一级v3
                    $parent = User::where(['id'=>$recommender->parent_id,'level'=>3])->field('id,level,parent_id')->find();

                    if ($parent){
                        ExtendConsume::addLog($consume_log_id,$parent->id,$v3_first_level_1_user_commission);
                        $send_total_commission+=$v3_first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);
                        //查当前推广人属于*的二级v3
                        $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>3])->field('id,level,parent_id')->find();
                        if ($parent_2){
                            ExtendConsume::addLog($consume_log_id,$parent_2->id,$v3_second_level_1_user_commission);
                            $send_total_commission+=$v3_second_level_1_user_commission;
                            array_push($extend_user_ids,$parent_2->id);
                        }
                    }
                }

            }

        }
        return false;


    }

    //任何店-折扣买单  type0是线下  1是折扣  2是线上
    public static function recommendShopConsumeCalculate($user_id,$shop_id,$total_commission,$consume_log_id,$type=1)
    {
        $shop_fans = ShopFans::where(['user_id'=>$user_id])->find();
        if ($shop_fans){
            $send_total_commission = 0;
            $extend_user_ids = [];

            $shop_info = Shop::alias('s')
                ->join('shop_user su','su.id = s.user_id')
                ->where(['s.shop_id'=>$shop_fans['shop_id']])
                ->field('su.recommender_id,s.shop_id')
                ->find();
            if ($type==1){
                $recommender_self_shop_commission = $total_commission*0.04;
                $first_level_1_user_commission = $total_commission*0.01;
                $second_level_1_user_commission = $total_commission*0.005;

                $v3_recommender_self_shop_commission = $total_commission*0.095;
                $v3_first_level_1_user_commission = $total_commission*0.01;//
                $v3_second_level_1_user_commission = $total_commission*0.005;//
                $second_rate_outside = 0.04;
                $second_rate = 0.045;
                $first_rate = 0.055;
            } else {
                $recommender_self_shop_commission = $total_commission*0.02;
                $first_level_1_user_commission = $total_commission*0.01;
                $second_level_1_user_commission = $total_commission*0.005;

                $v3_recommender_self_shop_commission = $total_commission*0.075;
                $v3_first_level_1_user_commission = $total_commission*0.01;//
                $v3_second_level_1_user_commission = $total_commission*0.005;//
                $second_rate_outside = 0.04;
                $second_rate = 0.045;
                $first_rate = 0.055;
            }


            if ($shop_id!=$shop_info['shop_id']){
                //不在本店消费发放用户所绑定店铺的9%
                $shop_commission = $total_commission*0.09;
                ExtendConsumeShop::addLog($consume_log_id,$shop_info['shop_id'],$shop_commission);

                //查询所属店铺的推广人
                $recommender = User::where(['id'=>$shop_info['recommender_id']])
                    ->field('id,level,parent_id')->find();

                if ($recommender){

                    $res = self::diffLastArr($recommender->id);

                    $last_arr = $res['list'];

                    $v3_weizhi = 0;
                    $v3_id = 0;

                    foreach ($last_arr as $k=>$v){
                        if ($v['level']==3&&$v['id']!=$recommender->id){
                            $v3_weizhi = $k;
                            $v3_id = $v['id'];
                            break;
                        }
                    }

                    if ($recommender->level==2){
                        //当前推广人收益

                        /*if ($shop_id==$shop_info['shop_id']){
                            $recommender_self_shop_commission = $total_commission*0.10;
                        } else {
                            $recommender_self_shop_commission = $total_commission*0.04;
                        }*/
                        //$recommender_self_shop_commission = $total_commission*0.10;
                        ExtendConsume::addLog($consume_log_id,$recommender['id'],$recommender_self_shop_commission);

                        //查当前推广人属于*的一级v2
                        $parent = User::where(['id'=>$recommender->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                        if ($parent){

                            ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                            $send_total_commission+=$first_level_1_user_commission;
                            array_push($extend_user_ids,$parent->id);

                            $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                            //查当前推广人属于*的二级v2
                            if ($parent_2){

                                ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                                $send_total_commission+=$second_level_1_user_commission;
                                array_push($extend_user_ids,$parent_2->id);
                            }
                        }

                        if ($v3_id){
                            if ($v3_weizhi>2) {
                                //二级外
                                $rate = $second_rate_outside;

                                $commission = $total_commission*$rate;

                                //$send_total_commission+=$commission;
                            } elseif($v3_weizhi==2){
                                //二级v1v2
                                $rate = $second_rate;
                                $commission = $total_commission*$rate;
                            } elseif($v3_weizhi==1){
                                //一级v1v2
                                $rate = $first_rate;
                                $commission = $total_commission*$rate;
                            }
                            ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                        }

                    } elseif ($recommender->level==3){
                        //v3 只有一级v3二级v3吃,
                        ExtendConsume::addLog($consume_log_id,$recommender['id'],$v3_recommender_self_shop_commission);

                        //查当前推广人属于*的一级v3
                        $parent = User::where(['id'=>$recommender->parent_id,'level'=>3])->field('id,level,parent_id')->find();

                        if ($parent){
                            ExtendConsume::addLog($consume_log_id,$parent->id,$v3_first_level_1_user_commission);
                            $send_total_commission+=$v3_first_level_1_user_commission;
                            array_push($extend_user_ids,$parent->id);
                            //查当前推广人属于*的二级v3
                            $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>3])->field('id,level,parent_id')->find();
                            if ($parent_2){
                                ExtendConsume::addLog($consume_log_id,$parent_2->id,$v3_second_level_1_user_commission);
                                $send_total_commission+=$v3_second_level_1_user_commission;
                                array_push($extend_user_ids,$parent_2->id);
                            }
                        }
                    }

                }
            }
        }
        return true;
    }

    //霸王餐-推广商家
    public static function dineDashConsumeCalculate($user_id,$total_commission,$consume_log_id)
    {
        $user = User::get($user_id);

        //$operable_commission = bcsub($total_commission,$commission,2);

        if ($user){

            $first_level_1_user_commission = $total_commission*0.25;
            $second_level_1_user_commission = $total_commission*0.02;

            $v3_first_level_1_user_commission = $total_commission*0.04;
            $v3_second_level_1_user_commission = $total_commission*0.02;
            $second_rate_outside = 0.31;
            $second_rate = 0.33;
            $first_rate = 0.58;

            $res = self::diffLastArr($user->id);

            $last_arr = $res['list'];

            $v3_weizhi = 0;
            $v3_id = 0;

            foreach ($last_arr as $k=>$v){
                if ($v['level']==3&&$v['id']!=$user->id){
                    $v3_weizhi = $k;
                    $v3_id = $v['id'];
                    break;
                }
            }
            if ($user->level==1){
                //查当前用户属于*的一级v1
                $parent = User::where(['id'=>$user->parent_id,'level'=>1])->field('id,level,parent_id')->find();

                if ($parent){

                    ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);

                    array_push($extend_user_ids,$parent->id);

                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>1])->field('id,level,parent_id')->find();
                    //查当前推广人属于*的二级v2
                    if ($parent_2){

                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }

                if ($v3_id){
                    if ($v3_weizhi>2) {
                        //二级外
                        $rate = $second_rate_outside;

                        $commission = $total_commission*$rate;

                        //$send_total_commission+=$commission;
                    } elseif($v3_weizhi==2){
                        //二级v1v2
                        $rate = $second_rate;
                        $commission = $total_commission*$rate;
                    } elseif($v3_weizhi==1){
                        //一级v1v2
                        $rate = $first_rate;
                        $commission = $total_commission*$rate;
                    }
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                }

            } elseif ($user->level==2){

                //查当前用户属于*的一级v2
                $parent = User::where(['id'=>$user->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                if ($parent){

                    ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);

                    array_push($extend_user_ids,$parent->id);

                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                    //查当前推广人属于*的二级v2
                    if ($parent_2){

                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                        array_push($extend_user_ids,$parent_2->id);
                    }
                }

                if ($v3_id){
                    if ($v3_weizhi>2) {
                        //二级外
                        $rate = $second_rate_outside;

                        $commission = $total_commission*$rate;

                        //$send_total_commission+=$commission;
                    } elseif($v3_weizhi==2){
                        //二级v1v2
                        $rate = $second_rate;
                        $commission = $total_commission*$rate;
                    } elseif($v3_weizhi==1){
                        //一级v1v2
                        $rate = $first_rate;
                        $commission = $total_commission*$rate;
                    }
                    ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                }

            } elseif ($user->level==3){


                //查当前推广人属于*的一级v3
                $parent = User::where(['id'=>$user->parent_id,'level'=>3])->field('id,level,parent_id')->find();

                if ($parent){
                    ExtendConsume::addLog($consume_log_id,$parent->id,$v3_first_level_1_user_commission);

                    array_push($extend_user_ids,$parent->id);
                    //查当前推广人属于*的二级v3
                    $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>3])->field('id,level,parent_id')->find();
                    if ($parent_2){
                        ExtendConsume::addLog($consume_log_id,$parent_2->id,$v3_second_level_1_user_commission);

                        array_push($extend_user_ids,$parent_2->id);
                    }
                }
            }

        }

        return true;
    }

    //霸王餐-推广团队
    public static function dineDashShopConsumeCalculate($user_id,$total_commission,$consume_log_id)
    {
        $shop_fans = ShopFans::where(['user_id'=>$user_id])->find();

        if ($shop_fans){
            $send_total_commission = 0;
            $extend_user_ids = [];

            $shop_info = Shop::alias('s')
                ->join('shop_user su','su.id = s.user_id')
                ->where(['s.shop_id'=>$shop_fans['shop_id']])
                ->field('su.recommender_id,s.shop_id')
                ->find();
            if (!$shop_info) return false;
            $recommender_self_shop_commission = $total_commission*0.18;
            $first_level_1_user_commission = $total_commission*0.02;
            $second_level_1_user_commission = $total_commission*0.01;

            $v3_recommender_self_shop_commission = $total_commission*0.18;
            $v3_first_level_1_user_commission = $total_commission*0.02;//
            $v3_second_level_1_user_commission = $total_commission*0.01;//
            $second_rate_outside = 0.01;
            $second_rate = 0.02;
            $first_rate = 0.25;


            //查询所属店铺的推广人
            $recommender = User::where(['id'=>$shop_info['recommender_id']])
                ->field('id,level,parent_id')->find();

            if ($recommender){

                $res = self::diffLastArr($recommender->id);

                $last_arr = $res['list'];

                $v3_weizhi = 0;
                $v3_id = 0;

                foreach ($last_arr as $k=>$v){
                    if ($v['level']==3&&$v['id']!=$recommender->id){
                        $v3_weizhi = $k;
                        $v3_id = $v['id'];
                        break;
                    }
                }

                if ($recommender->level==2){
                    //当前推广人收益

                    ExtendConsume::addLog($consume_log_id,$recommender['id'],$recommender_self_shop_commission);

                    //查当前推广人属于*的一级v2
                    $parent = User::where(['id'=>$recommender->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();

                    if ($parent){

                        ExtendConsume::addLog($consume_log_id,$parent->id,$first_level_1_user_commission);
                        $send_total_commission+=$first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);

                        $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>['in',[1,2]]])->field('id,level,parent_id')->find();
                        //查当前推广人属于*的二级v2
                        if ($parent_2){

                            ExtendConsume::addLog($consume_log_id,$parent_2->id,$second_level_1_user_commission);
                            $send_total_commission+=$second_level_1_user_commission;
                            array_push($extend_user_ids,$parent_2->id);
                        }
                    }

                    if ($v3_id){
                        if ($v3_weizhi>2) {
                            //二级外
                            $rate = $second_rate_outside;

                            $commission = $total_commission*$rate;

                            //$send_total_commission+=$commission;
                        } elseif($v3_weizhi==2){
                            //二级v1v2
                            $rate = $second_rate;
                            $commission = $total_commission*$rate;
                        } elseif($v3_weizhi==1){
                            //一级v1v2
                            $rate = $first_rate;
                            $commission = $total_commission*$rate;
                        }
                        ExtendConsume::addLog($consume_log_id,$v3_id,$commission);
                    }

                } elseif ($recommender->level==3){
                    //v3 只有一级v3二级v3吃,
                    ExtendConsume::addLog($consume_log_id,$recommender['id'],$v3_recommender_self_shop_commission);

                    //查当前推广人属于*的一级v3
                    $parent = User::where(['id'=>$recommender->parent_id,'level'=>3])->field('id,level,parent_id')->find();

                    if ($parent){
                        ExtendConsume::addLog($consume_log_id,$parent->id,$v3_first_level_1_user_commission);
                       // $send_total_commission+=$v3_first_level_1_user_commission;
                        array_push($extend_user_ids,$parent->id);
                        //查当前推广人属于*的二级v3
                        $parent_2 = User::where(['id'=>$parent->parent_id,'level'=>3])->field('id,level,parent_id')->find();
                        if ($parent_2){
                            ExtendConsume::addLog($consume_log_id,$parent_2->id,$v3_second_level_1_user_commission);
                            ///$send_total_commission+=$v3_second_level_1_user_commission;
                            array_push($extend_user_ids,$parent_2->id);
                        }
                    }
                }

            }

        }
        return true;
    }

    //查用户上级的第一个1级2级v1/v2
    public static function getIds($id,$level){
        $user = User::where(['id'=>$id])->field('id,parent_id,nickname,level,avatar,mobile')->find();
        if ($user){
            if ($user->parent_id>0){

                if ($user->level==$level){
                    return $user;
                } elseif ($user->level==3&&$level!=3){
                    return '';
                } else {
                    return self::getIds($user->parent_id,$level);
                }
            } else {
                return $user;
            }
        } else {
            return '';
        }

    }

    //获取所有的上级
    public static function getLastList($id,$arr){
        $parent = User::get($id);
        array_push($arr,['id'=>$parent->id,'level'=>$parent->level]);
        if ($parent->parent_id==0){
            return $arr;
        } else {
            return self::getLastList($parent->parent_id,$arr);
        }
    }

    //将所有上级用户根据等级区分成数组
    public static function diffLastArr($user_id)
    {
        $list = self::getLastList($user_id,[]);
        $result = array();
        // php二维数据某个user_id字段值相同则放到一起,php数组根据某键值，把相同键值的合并最终生成一个新的二维数组.. key 为user_id
        foreach ($list as $key => $info) {
            $result[$info['level']][] = $info;
        }

        return ['diff_arr'=>$result,'list'=>$list];
    }

}